﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows;
using System.Threading;
using BAL.EstRec;
using System.Data;

namespace Nav3
{
    class PublicarHQ
    {
        public static Thread t = null;

        public static bool CheckIsAlive()
        {

            if (PublicarHQ.t != null)
            {
                return PublicarHQ.t.IsAlive;

            }
            return false;

        }/////////

        public static void start(string unidVrs, CapturaHq cc)
        {
            //if (!PopupNotify.CheckIsAlive())
            //{
            PublicarHQ.t = new Thread(() =>
            {
                Action a = () => updateStatus(unidVrs, cc);
                Application.Current.Dispatcher.BeginInvoke(a, System.Windows.Threading.DispatcherPriority.Background);
            });

            PublicarHQ.t.IsBackground = true;
            PublicarHQ.t.Start();
            //}
        }

        protected static void updateStatus(string unidVrs, CapturaHq cc)
        {
            string[] columnas = { "UNID_HQBGT_VERSION", "UNID_HQBGT", "ID_POS", "ID_RECURSO", "CANTIDAD", "NAME", "IS_PUBLISH", "IS_CONSOLIDATED", "ID_STATUS", "WIN_USER_CREATOR", "APP_PROFILE" };
            char separador = '\t';

            AppConfs ac = new AppConfs();

            cc.btnPublicar.IsEnabled = false;

            string rutaGuardar = ac.cnf.ClientHQ;
            string rutaCopiar = ac.cnf.ServerHQ;
            try
            {
                PublicarTXT pTXT = new PublicarTXT(false, columnas, separador, rutaGuardar, rutaCopiar, unidVrs);

                bool aux1 = false;
                bool aux2 = false;
                BgtVersion bgt;

                aux1 = pTXT.exportHQ();

                if(aux1)
                    aux2 = pTXT.exportHQcopiar();

                bgt = new BgtVersion(long.Parse(unidVrs), aux1, aux2);

                if (BgtVersion.IsSend(bgt))
                {
                    cc.btnPublicar.IsEnabled = true;
                    cc.lbl_versionStat.Content = "";
                    cc.IsSend = true;

                    if (BgtVersion.IsPublishSharedPoint(bgt))
                    {
                        cc.btnPublicar.IsEnabled = false;
                        cc.lbl_versionStat.Content = "Versión publicada.";
                        cc.IsPublish = true;
                    }
                    else
                        cc.lbl_versionStat.Content = "No se pudo completar la publicación. Vuelva a intentar";
                }
                                
                cc.readOnly2(cc.IsPublish, cc.IsSend);
            }
            catch (Exception ex)
            {
                cc.lbl_versionStat.Content = ex.Message;
            }

            Console.WriteLine("sale del hilo");

        }

        public static bool checkVersionHasData(string unidVersion)
        {
            string query = "SP_ER_GET_VERSIONCNTDATA @UNID_VERSION=" + unidVersion;
            int count = 0;
            DataTable dt = new DataTable();

            try
            {
                dt= DAL.EstRec.SrvDB.ExecuteQuery(query);
                if (dt.Rows.Count > 0)
                {
                    count = int.Parse(dt.Rows[0][0].ToString());
                    if (count > 0)
                        return true;
                    else return false;
                }
                else return false;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error al validar existencia de datos de versión.\n"+ex.Message);
                return  false;
            }
            
        }//

    }
}
